package com.dmall.opening.up.api.endpoint;

import com.dmall.opening.up.core.OrderRepository;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import javax.annotation.Resource;

@RestController
@RequestMapping("orders/{order_id}")
public class OrderItemController {

    @Resource
    private OrderRepository orders;

    @PutMapping("items/{item_id}/return")
    public void _return(@PathVariable("order_id") Long orderId,
                        @PathVariable("item_id") Long itemId) {
        orders.findOne(orderId)
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND))
                .findItem(itemId)
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND))
                .return_();
    }
}
